// pages/index/record/index.js
import Toast from '../../../../miniprogram_npm/@vant/weapp/toast/toast';
const $api = require('../../../../request/api').API;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 记录
    dataList: [],
    // tabs的下标
    tabsIndex: 1,
    //分页相关
    pageNum: 1,
    pageSize: '',
    total: '',
    totalPage: '',
    // 筛选
    accessList: [
      { accessName: '全部', id: 0 },
      { accessName: '1个月', id: 1 },
      { accessName: '3个月', id: 3 },
      { accessName: '半年', id: 6 },
      { accessName: '一年', id: 12 }
    ],
    accessIndex: 0,
    accessName: '',
    screeningValue: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getList();
  },
  // 切换tabs
  onClick(e) {
    let index = e.detail.index;
    this.setData({
      accessIndex: 0,
      accessName: '',
      tabsIndex: index + 1,
      pageNum: 1
    });
    this.getList();
  },
  //获取记录
  getList() {
    let params = {
      billType: this.data.tabsIndex,
      pageNo: this.data.pageNum,
      pageSize: 10
    };
    $api
      .beeperBill(params)
      .then(res => {
        this.setData({
          dataList: res.data.data.list,
          pageNum: res.data.data.pageNum,
          pageSize: res.data.data.pageSize,
          total: res.data.data.total,
          totalPage: res.data.data.totalPage
        });
      })
      .catch(err => {
        console.log('充值记录', err);
      });
  },
  //选择渠道来源事件
  bindPickerChangeAccess: function (e) {
    var accessObj = this.data.accessList[e.detail.value];
    this.setData({
      accessIndex: e.detail.value,
      accessName: accessObj.accessName
    });

    let index = this.data.accessIndex;
    var screeningValue = '';
    switch (index) {
      case '0':
        screeningValue = 0;
        break;
      case '1':
        screeningValue = 1;
        break;
      case '2':
        screeningValue = 3;
        break;
      case '3':
        screeningValue = 6;
        break;
      case '4':
        screeningValue = 12;
        break;
      default:
        screeningValue = 0;
        break;
    }
    this.setData({
      screeningValue: screeningValue
    });
    console.log(this.data.screeningValue);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.setData({
      pageNum: this.data.pageNum + 1
    });
    let params = {
      billType: this.data.tabsIndex,
      pageNo: this.data.pageNum,
      pageSize: 10
    };
    let dataList = this.data.dataList;
    var dataConcat = [];
    $api
      .beeperBill(params)
      .then(res => {
        let list = res.data.data.list;
        console.log('分页出来的list', list);
        if (list.length != 0) {
          dataConcat = dataList.concat(list);
          this.setData({
            dataList: dataConcat
          });
        } else {
          Toast('没有更多啦');
        }
        console.log('合并后', this.data.dataList);
      })
      .catch(err => {
        console.log('充值记录', err);
      });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
});
